<template>
  <div class="login-container">
    <!-- <Top /> -->
    <el-card class="z-1 !border-none w100 !bg-transparent !rounded-[4%] <sm:w83 loginForm">
      <div style="display:flex">
        <h3 class="mt6 font-medium text-primary text-[18px]" type="primary">{{ $t('login.login') }}</h3>
        <div class="left">
          <el-button v-if='IsShowPhone' @click="changeLoginType">手机号登录</el-button>
          <el-button v-else @click="changeLoginType">账号密码登录</el-button>
        </div>
      </div>
      <!-- 密码登录 -->
      <el-form ref="formRef" :model="form" :rules="formRules" class="mt6" size="large" v-if="IsShowPhone">
        <el-form-item prop="phone">
          <el-input v-model="form.phone" :placeholder="$t('login.usernamePlaceholder')" maxlength="11" @keyup.enter="onLogin" />
        </el-form-item>

        <el-form-item prop="imageCode" class="flex">
          <el-input v-model="form.imgCode" placeholder="图片验证码" style="flex: 1;"  @keyup.enter="onLogin"></el-input>
          <div class="img_box"  :style="{backgroundImage: `url(${imgCodeUrl})`}"  @click="getImgPic"></div>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="form.password" show-password :placeholder="$t('login.passwordPlaceholder')" @keyup.enter="onLogin" />
        </el-form-item>
        <el-form-item>
          <div class="w-full flex-y-center justify-between">
            <el-checkbox v-model="checked" class="<sm:text-sm">
              {{ $t('login.remember') }}
            </el-checkbox>
            <!-- <el-text type="primary" class="cursor-pointer <sm:!text-sm">
              {{ $t('login.forgetPassword') }}
            </el-text> -->
          </div>
        </el-form-item>
        <el-form-item>
          <el-button class="w-full" type="primary" @click="onLogin" :loading="loading">
            {{ $t('login.login') }}
          </el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button class="w-full"> {{ $t('login.register') }}</el-button>
        </el-form-item> -->
      </el-form>
      <!-- 手机号登录 -->
      <otherLogin v-if="!IsShowPhone"></otherLogin>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onBeforeMount,onMounted } from 'vue';
import { Top } from './components';
import { formRules } from './helpers/rule';
import otherLogin from '@/views/login/components/otherLogin.vue'
import { useUserStore,useRouteStore } from '@/store';
import { getImgCode } from '@/api/auth';

defineOptions({ name: 'Login' });
const IsShowPhone = ref(true);
const changeLoginType = () => {
  IsShowPhone.value = !IsShowPhone.value
}

const formRef = ref();
const checked = ref(true);
const loading = ref(false);
const userStore = useUserStore();
const routeStore = useRouteStore();


const imgCodeUrl = ref();

const form = reactive({
  phone: '',
  password: '',
  imgCode: ''
});
// if(checked){
  
// }else{
//   form.phone=''
//   form.password=''
// }


function fist(){
  // 检查本地存储中是否有保存的用户名和密码
  if (localStorage.getItem("username") && localStorage.getItem("password")) {
      form.phone = localStorage.getItem("username");
      form.password = localStorage.getItem("password");
    }
}
onMounted(() => {
  fist()
});
onBeforeMount(() => {
  getImgCodeFun()
})
const getImgPic = () => {
  getImgCodeFun()
}

const onLogin = async () => {
  await formRef.value?.validate();
  try {
    loading.value = true;
    await userStore.login(form);
    
    routeStore.setUserInfo()
    // 如果用户勾选了记住密码，则保存到本地存储中
    if (checked) {
      localStorage.setItem("username", form.phone);
      localStorage.setItem("password", form.password);
    } else {
      localStorage.removeItem("username");
      localStorage.removeItem("password");
    }
  } catch(e) {
    getImgPic()
  } finally {
    
    loading.value = false;
  }
};

const getImgCodeFun = async () => {
  const data: any = await getImgCode({});
  
  // console.log(data)
  imgCodeUrl.value = window.URL.createObjectURL(data);
};
</script>

<style lang="scss" scoped>
.login-container {
  @apply relative wh-full flex-center dark:bg-#101628 bg-no-repeat bg-center-top;
  background-image: url('/src/assets/image/loginBg1.jpg');
  background-size: 100% 100%;

  .loginForm {
    position: absolute;
    right: 10rem;
    .left {
      flex: 1;
      justify-content: flex-end;
      display: flex;
      align-items: center;
      text-align: initial;
      margin-top: 15px;
    }
  }
}
.img_box{
  width: 100px; 
  height:40px;
  margin-left: 5px;
  background-position: center center;
  background-size: 150%;
  background-repeat: no-repeat;
}
</style>
